package cn.train.controler;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.train.service.AddOrderTableService;
import cn.train.service.VipCoderAndPassServices;

@WebServlet("/VipChangeStateServlet")
public class VipChangeStateServlet extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id=req.getParameter("id");
		String flag=req.getParameter("flag");
		
		
		String zuotype=req.getParameter("zuotype");
		String actdate=req.getParameter("date");
		String actdateriqi=req.getParameter("da");
		String car=req.getParameter("car");
		String name=req.getParameter("name");
		System.out.println("出发日期"+actdateriqi);
		PrintWriter out=resp.getWriter();
		java.sql.Date dat=null;
		int fla=0;
		if(flag.equals("tui")){
			Date date=new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd");
			
			
			try {
				dat = new java.sql.Date( sdft.parse(actdateriqi).getTime());
				Date acd= sdf.parse(actdate);
				if((acd.getTime()-date.getTime())<=0) {
					out.print(2);
					return;
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			AddOrderTableService atl = new AddOrderTableService();
			fla=atl.panDuanPiao(zuotype, -1, dat, car);
		}
		
		
		int state=0;
		if(flag.equals("out")){
			state=2;
		}else{
			state=3;
		}
		int oid=Integer.parseInt(id);
		VipCoderAndPassServices vaps=new VipCoderAndPassServices();
		int m=vaps.updatestate(state, oid);
		
		System.out.println("m==="+m+"flag=="+fla);
		
		if(flag.equals("tui")){
			if(m>0&fla>0){
				out.print(1);
				return;
			}else{
				out.print(0);
				return;
			}
		}
		if(m>0){
			out.print(1);
			return;
		}else{
			out.print(0);
			return;
		}
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
